package com.example.bysj.Service;
//201902104061徐中堂
import com.example.bysj.Dao.*;
import com.example.bysj.domain.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Slf4j
//声明一个业务处理类
@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ActorDao actorDao;

    /**
     * 增加学生
     * @param student
     * @return
     */
    public String addStudent(Student student){
        int id = actorDao.add();
        student.setId(id);
        boolean add = studentDao.addStudent(student);
        if (add){
            return "增加成功";
        } else
            return "增加失败";
    }

    /**
     * 批量增加学生
     * @param students
     * @return
     */
    public String addStudents(List<Student> students){
        boolean added = false;
        for (int i = 0; i < students.size(); i++){
            int id = actorDao.add();
            students.get(i).setId(id);
            added =  studentDao.addStudent(students.get(i));
        }
        if (added){
            return "增加成功";
        } else
            return "增加失败";
    }

    /**
     * 查找所有
     * @return
     */
    public List<Student> getAll(){
        return studentDao.getAll();
    }

    /**
     * 根据编号查找
     * @param id
     * @return
     */
    public Student getOneById(int id){
        return studentDao.getOneById(id);
    }

    /**
     * 更新学生信息
     * @param student
     * @return
     */
    public String updateStudent(int id,Student student){
        if (studentDao.updateStudent(id,student)){
            return "更新成功";
        }else {
            log.error("更新失败" + id + "不存在！");
            return "更新失败" + id + "不存在！";
        }
    }

    /**
     * 删除学生信息
     * @param id
     * @return
     */
    public String deleteStudent(int id){
        if (studentDao.deleteStudent(id)){
            return "删除成功";
        }else {
            log.error("删除失败" + id + "不存在！");
            return "删除失败" + id + "不存在！";
        }
    }
}
